<template>
  <div class='tinymce'>
    <h4 style="margin:0 0 20px 0">
      <el-row>
        <el-col :span="19">
          <span v-if="dataPush">编辑商品</span>
          <span v-else>添加商品</span>
        </el-col>
        <el-col :span="5" style="text-align:right">
          <i class="el-icon-error" style="background:#e6e6e6" @click="goBack"></i>
        </el-col>
      </el-row>
    </h4>
    <div>
      
     <el-form  label-position="left" label-width="80px" :rules="rules" ref="formAling" :model="formLabelAlign">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formLabelAlign.title"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="title">
          <el-select v-model="formLabelAlign.typeId" placeholder="请选择">
            <el-option v-for="item in otypes" :key="item.id" :label="item.name" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签" prop="title">
           <el-checkbox-group v-model="tagsValue">
            <el-checkbox v-for="item in tags" :key="item.name"  :label="item.name"></el-checkbox>
            
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="formLabelAlign.number" style="width:200px"></el-input>
          <span>原价：</span>
           <el-input v-model="formLabelAlign.number2" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="有效期" prop="push_time">
          <el-date-picker
            v-model="formLabelAlign.push_time"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间">
          </el-date-picker>
          <span style="color:#ccc">过期后用户未消费将自动退款</span>
        </el-form-item>
        <el-form-item label="封面" prop="title">
          <el-upload 
            class="avatar-uploaderInfo"
            action="https://upload.qiniup.com/"
            :show-file-list="false"
            :on-success="handleOkLogo"
            :before-upload="beforeAvatarUpload"
            :data="postData">
            <img v-if="formLabelAlign.header_url" :src="formLabelAlign.header_url" class="avatarInfo">
            <i v-else class="el-icon-plus avatar-uploader-iconInfo"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      
      
    
    </div>
    
  </div>
</template>

<script>
import Http from '@/Http/Http'
import tinymce from 'tinymce/tinymce'
import 'tinymce/themes/modern/theme'
import Editor from '@tinymce/tinymce-vue'


export default {
  name: 'tinymce',
  props:['dataPush'],
  data () {
    const title = (rule,value,callback) => {
      if (value === '') {
        callback(new Error('请输入标题'))
      } else {
        callback()
      }
    }
    
    return {
     
      postData:{
        token:'',
      },
      tokenimgUrl:'',
      dialogImageUrl: '',
      dialogVisible: false,
      otypes:'', //类型
      tags:'', //标签
      tagsValue:[],
      formLabelAlign:{
        title:'',
        typeId:'', //类型
        is_timer:'1',
        push_time:'',
        header_url:'' //封面
      },
      rules:{
        title:[
          {validator:title,trigger:'blur'}
        ]
      },

      imgstuts:false, //富文本编辑器部分
      fileList:[],
      
    }
    
  },

  mounted () {
    
  },
  created() {
    console.log(this.dataPush)
    this.getToken()
  },
  methods:{
    getToken() {
      Http.QiniuToken({})
      .then( res => {
        this.postData.token = res.data.data.uptoken;
        this.tokenimgUrl = res.data.data.domain
      })
      Http.getCommoTypes()
      .then( res => {
        console.log(res.data.data)
        this.otypes = res.data.data
        console.log(this.otypes)
      })
      Http.getCommoTags()
      .then( res => {
        this.tags = res.data.data
      })
      .catch( error => {
        this.$router.push('/')
        console.log(error)
      });
      if (this.dataPush) {
        this.formLabelAlign.title = this.dataPush.title;
        this.formLabelAlign.is_timer = this.dataPush.is_timer;
        this.formLabelAlign.push_time = this.dataPush.push_time;
        this.formLabelAlign.is_timer = '1';
      }
    },
   
    addAPushFun(param) {
      console.log(param)
      console.log('添加文章')
      Http.addPush(param)
        .then( res => {
          if( res.data.code == 200 ){
            this.$message.success('推送发布成功')
            this.goBack()         
          } else if(res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            this.$router.push('/')
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
        })
    },
    EditPushFun(param) {
      param.id = this.dataPush.id;
      Http.editPush(param)
      .then( res => {
        if( res.data.code == 200 ){
          this.$message.success('推送修改成功')
          this.goBack()         
        } else if(res.data.code == -1003) {
          this.$message.error(`请求失败原因：${res.data.message}`)
          this.$router.push('/')
        }
      })
      .catch( error => {
        this.$message.error('服务器请求失败')
      })
    },
    handleOkLogo(responed) {
      console.log(responed)
      this.formLabelAlign.header_url = `http://${this.tokenimgUrl}/${responed.key}`;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      if (isJPG || isPNG) {
        return true;
      } else {
          this.$message.error('上传图片格式不正确!');
        return false;
      }
    },
    goBack(){
      this.$emit('getlist')
    },
    
    
  },
  components: {
  }
}
</script>
<style>
.el-textarea__inner{
  height:146px
}



.disabled .el-upload--picture-card {
    display: none;
}
.avatar-uploaderInfo .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploaderInfo .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-iconInfo {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}
.avatarInfo {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
